package com.cnhonker.edu.core.dao;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by Apple on 2017/6/9.
 */
@Repository("baseDao")
public class BaseDaoImpl implements IBaseDao{
    @Autowired
    public SqlSession sqlSession;

    public Long insert(String sqlKey, Object object) {
        return Long.valueOf((long)sqlSession.insert(sqlKey, object));
    }

    public Long delete(String sqlKey, Object object) {
        return Long.valueOf((long)sqlSession.delete(sqlKey, object));
    }

    public Long update(String key, Object object) {
        return Long.valueOf((long)sqlSession.update(key, object));
    }

    public <T> T selectOne(String sqlKey, Object params) {
        Object selectOne = null;
        List<Object> list = this.selectList(sqlKey, params);
        if(list != null && list.size() > 0) {
            selectOne = list.get(0);
        }

        return (T) selectOne;
    }

    public <T> List<T> selectList(String sqlKey, Object params) {
        return sqlSession.selectList(sqlKey, params);
    }
}
